package com.tyk.bookstore.back.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tyk.bookstore.back.common.core.model.exception.CustomException;
import com.tyk.bookstore.back.common.core.model.result.CommonResultEnum;
import com.tyk.bookstore.back.common.core.util.ObjUtil;
import com.tyk.bookstore.back.common.security.util.SecurityUtil;
import com.tyk.bookstore.back.system.model.dto.UserInfoDto;
import com.tyk.bookstore.back.system.model.dto.RoleDto;
import com.tyk.bookstore.back.system.model.dto.RouteDto;
import com.tyk.bookstore.back.system.model.dto.User4LoginCheckDto;
import com.tyk.bookstore.back.system.model.entity.UserDo;
import com.tyk.bookstore.back.system.model.query.ExistsPrincipalQuery;
import com.tyk.bookstore.back.system.model.query.UpdateUserInfoQuery;
import com.tyk.bookstore.back.system.model.query.User4LoginCheckQuery;
import com.tyk.bookstore.back.system.repository.MenuRepository;
import com.tyk.bookstore.back.system.repository.PermissionRepository;
import com.tyk.bookstore.back.system.repository.RoleRepository;
import com.tyk.bookstore.back.system.repository.UserRepository;
import com.tyk.bookstore.back.system.service.UserApiService;
import com.tyk.bookstore.back.system.util.MapstructSystem;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserApiServiceImpl implements UserApiService {

    private final MapstructSystem mapstructSystem;

    private final UserRepository userRepository;

    private final RoleRepository roleRepository;

    private final PermissionRepository permissionRepository;

    private final MenuRepository menuRepository;

    @Override
    public User4LoginCheckDto selectUserByPrincipal(User4LoginCheckQuery query) {
        return userRepository.selectUserByPrincipal(query);
    }

    @Override
    public Boolean existsPrincipal(ExistsPrincipalQuery query) {
        LambdaQueryWrapper<UserDo> w = Wrappers.lambdaQuery(UserDo.class);
        if (ObjUtil.notBlank(query.getAccount())) w.eq(UserDo::getAccount, query.getAccount());
        else if (ObjUtil.notBlank(query.getEmail())) w.eq(UserDo::getEmail, query.getEmail());
        else if (ObjUtil.notBlank(query.getPhone())) w.eq(UserDo::getPhone, query.getPhone());
        else throw new CustomException(CommonResultEnum.PARAM_ERROR);
        return userRepository.exists(w);
    }

    @Override
    public List<RoleDto> selectRoleDtoList(Long userId) {

        return roleRepository.selectRoleDtoList(userId);
    }

    @Override
    public List<String> selectPermissionList(List<Long> roleIdList) {
        return permissionRepository.selectPermissionList(roleIdList);
    }

    @Override
    public List<RouteDto> selectRouteDtoList(Long userId) {
        if (SecurityUtil.getRoot(userId)) {
            return menuRepository.selectAllGetRouteDto();
        } else {
            return menuRepository.selectGetRouteDtoListByUserId(userId);
        }

    }

    @Override
    public UserInfoDto selectUserInfoVo(Long loginId) {
        return userRepository.selectGetUserInfoVo(loginId);
    }

    @Override
    public void updateUserInfo(UpdateUserInfoQuery query) {
        UserDo userDo = mapstructSystem.toUserDo(query);
        userRepository.updateById(userDo);
    }

    @Override
    public String selectPasswordById(Long loginId) {
        LambdaQueryWrapper<UserDo> queryWrapper = Wrappers.lambdaQuery(UserDo.class);
        queryWrapper.select(UserDo::getPassword).eq(UserDo::getId, loginId);
        return userRepository.selectOne(queryWrapper).getPassword();
    }

}
